package com.mgq.java8.stream;

import com.mgq.java8.lambda.Apple;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <b>功能：</b>TestSort<br>
 * <b>Copyright mgq</b>
 * <li>版本&nbsp;&nbsp;&nbsp;&nbsp;修改日期&nbsp;&nbsp;&nbsp;&nbsp;部　　门&nbsp;&nbsp;&nbsp;&nbsp;作　者&nbsp;&nbsp;&nbsp;&nbsp;变更内容</li><hr>
 * <li>v1.0&nbsp;&nbsp;&nbsp;&nbsp;20220326&nbsp;&nbsp;营业后台&nbsp;&nbsp;&nbsp;&nbsp;马广奇&nbsp;&nbsp;&nbsp;&nbsp;创建类</li>
 * <br><br>
 *
 * @author 马广奇
 * @version 2022-03-26 22:16
 */
public class TestSort {
    public static   List<Transaction> transactions;
    static {
        Trader raoul = new Trader("Raoul", "Cambridge");
        Trader mario = new Trader("Mario", "Milan");
        Trader alan = new Trader("Alan", "Cambridge");
        Trader brian = new Trader("Brian", "Cambridge");
        transactions = Arrays.asList(
                new Transaction(brian, 2011, 300),
                new Transaction(raoul, 2012, 1000),
                new Transaction(raoul, 2011, 400),
                new Transaction(mario, 2012, 710),
                new Transaction(mario, 2012, 700),
                new Transaction(alan, 2012, 950)
        );
    }

    /**
     *测试
     */
    public static void testTrace() {
        //1.找出2011年发生的所有交易,并按交易额进行排序(从低到高)
        List<Transaction> collect = transactions.stream().filter(transaction -> transaction.getYear() == 2011)
                .sorted(Comparator.comparingInt(Transaction::getValue))
                .collect(Collectors.toList());
        //2.交易员都在那些不同的城市工作过
        List<String> collect1 = transactions.stream().map(transaction -> transaction.getTrader().getCity())
                .distinct().collect(Collectors.toList());
                //直接使用toSet
        Set<String> set = transactions.stream().map(transaction -> transaction.getTrader().getCity())
                .collect(Collectors.toSet());
        //3.查找所有来自剑桥的交易员,并按姓名排序
        List<Trader> collect2 = transactions.stream().map(Transaction::getTrader)
                .filter(trader -> "Cambridge".equals(trader.getCity()))
                .distinct()
                .sorted(Comparator.comparing(Trader::getName))
                .collect(Collectors.toList());
        //4.返回所有交易员的姓名字符串,按字母顺序排序
        String collect3 = transactions.stream().map(transaction -> transaction.getTrader().getName())
                .distinct()
                .sorted().reduce("", (n1, n2) -> n1 + n2);
                //方法二
        String collect32 = transactions.stream().map(transaction -> transaction.getTrader().getName())
                .distinct()
                .sorted().collect(Collectors.joining());
        //5.有没有交易员在米兰工作的?
        boolean present = transactions.stream().map(transaction -> transaction.getTrader().getCity())
                .anyMatch("Milan"::equals);
        //6.打印生活在剑桥的交易员的所有交易额
        transactions.stream().filter(transaction -> "Cambridge".equals(transaction.getTrader().getCity()))
                .map(Transaction::getValue)
                .forEach(System.out::println);
        //7.所有交易中,最高的交易额是多少?
        Optional<Integer> reduce1 = transactions.stream().map(Transaction::getValue).reduce(Integer::max);
        //8.找到交易额最小的交易
        Optional<Transaction> reduce = transactions.stream().reduce((t1, t2) -> t1.getValue() < t2.getValue() ? t1 : t2);
                //方法二
        Optional<Transaction> min = transactions.stream().min(Comparator.comparing(Transaction::getValue));
    }
    /**
     * 比较器复合
     */
    public static void testSort() {
        List<Apple> list = Arrays.asList(new Apple("green", 100), new Apple("red", 160),
                new Apple("green", 155), new Apple("red", 101));
        //排序
        list.sort(Comparator.comparing(Apple::getWeight));
        //逆序
        list.sort(Comparator.comparing(Apple::getWeight).reversed());
        //比较器链.  2个苹果重量相等时,再按照颜色排序
        list.sort(Comparator.comparing(Apple::getWeight).reversed().thenComparing(Apple::getColor));
    }
}
